<?php 
class IpBlacklistDao
{
	public function __construct()
	{
		$this->_tgIpBlacklist = Db::newDbTable("ip_blacklist");
	}
	
	public function insertIp($ipRange)
	{		
		$ipRange["begin_ip"] = ip2long($ipRange["begin_ip"]);
		$ipRange["end_ip"] = ip2long($ipRange["end_ip"]);
		return $this->_tgIpBlacklist->insert($ipRange);
	}
	
	public function deleteIp($recordId)
	{
		return $this->_tgIpBlacklist->delete($recordId);
	}
	
	public function getBlacklistByIp($ip)
	{
		$condition["where"]["expression"] = "begin_ip <= :begin_ip AND end_ip >= :end_ip";
		$condition["where"]["value"] = array("begin_ip" => ip2long($ip), "end_ip" => ip2long($ip));
		$foundBlacklists = array();
		foreach ($this->_tgIpBlacklist->fetchRows($condition) as $blacklist)
		{
			$blacklist["begin_ip"] = long2ip($blacklist["begin_ip"]);
			$blacklist["end_ip"] = long2ip($blacklist["end_ip"]);
			$foundBlacklists[] = $blacklist;
		}
		return $foundBlacklists;
	}
}